<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>    
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.hkunderground.blog.helper.ConstantHelper" %>
<%@ page import="com.hkunderground.blog.helper.UtilityHelper" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>>> > hkunderground < <<</title>
</head>
<body>
<%
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key loBlogKey = KeyFactory.createKey(ConstantHelper.KEY_KIND_BLOG, ConstantHelper.KEY_NAME_BLOG_HKUG);
    // Run an ancestor query to ensure we see the most up-to-date
    // view of the Greetings belonging to the selected Guestbook.
    Query query = new Query(ConstantHelper.ENTITY_BLOG, loBlogKey).addSort(ConstantHelper.E_BLOG_DATE, Query.SortDirection.DESCENDING);
    List<Entity> loBlogList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    
    if (loBlogList.isEmpty()) {
        %>
        <p>no content.</p>
        <%
    } else {
        %>
        <p>Da Content:</p>
        <%
        for (Entity blog : loBlogList) {
%>
            <p><b><%= blog.getProperty(ConstantHelper.E_BLOG_TITLE) %></b> by <%= ((User) blog.getProperty(ConstantHelper.E_BLOG_USER)).getNickname() %></p>
            <%= UtilityHelper.getDateMMMMDDYYYY_24H((Date)blog.getProperty(ConstantHelper.E_BLOG_DATE)) %>
            <br>
            <blockquote><%= blog.getProperty(ConstantHelper.E_BLOG_CONTENT) %></blockquote>
			<br>
			Tag(s): 
<%
			Query lqBTRel = new Query(ConstantHelper.ENTITY_BLOGTAG_REL);
			lqBTRel.setAncestor(blog.getKey());
			
			List<Entity> leBTRelList = datastore.prepare(lqBTRel).asList(FetchOptions.Builder.withDefaults());
			
			for (Entity blogTag : leBTRelList)
			{
				Key lkTagKey = (Key) blogTag.getProperty(ConstantHelper.E_BLOGTAG_REL_TAG_KEY);
				Query lqTag = new Query(ConstantHelper.ENTITY_TAG, lkTagKey);
				Entity leTag = datastore.prepare(lqTag).asSingleEntity();
%>
				[<%= leTag.getProperty(ConstantHelper.E_TAG_NAME) %>]				
<%
			}//for E_BlogTag

        }//for
    }//if
%>

</body>
</html>